package com.sky.task;

import com.sky.context.BaseContext;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @Author: WangHongKun
 * @Date: 2022/9/22 16:58
 * @Email: 2028911483@qq.com
 * @Phone: 18683977706
 */

@Slf4j
@Component
public class OrderTask {

    @Autowired
    private OrderMapper orderMapper;

    @Scheduled(cron = "0 0/1 * * * ?")
    public void processOverTimeOrders(){
        log.info("处理超时订单的时间：{}", new Date());
        List<Orders> ordersList = orderMapper.getByStatusAndTime(Orders.PENDING_PAYMENT,LocalDateTime.now().minusMinutes(15));
        if(ordersList == null && ordersList.size() ==0){
            return;
        }
        for(Orders orders : ordersList){
            orders.setStatus(Orders.CANCELLED);
            orders.setCancelReason("订单超时未支付");
            orders.setCancelTime(LocalDateTime.now());
            orderMapper.update(orders);
        }
    }

    @Scheduled(cron = "0 0 23 * * ?")
    public void processDeliveryOrders(){
        log.info("处理超时派送订单的时间：{}", new Date());
        List<Orders> ordersList = orderMapper.getByStatus(Orders.DELIVERY_IN_PROGRESS);
        if(ordersList == null && ordersList.size() ==0){
            return;
        }
        for(Orders orders : ordersList){
            orders.setStatus(Orders.COMPLETED);
            orderMapper.update(orders);
        }
    }
}
